<?php

namespace dk\widget;

abstract class Base{
    protected $name;

    abstract function bindHandle($plug);

    /**
     * @var \dk\core\View
     */
    protected $view;
    function setView($view){
        $this->view = $view;
    }

    protected $request = [];
    function setRequest($request){
        $this->request = $request;
        return $this;
    }

    function q($k, $def = null){
        if(isset($this->request[$k])){
            return $this->request[$k];
        }
        return $def;
    }

    function getName(){
        if(!isset($this->name)){
            $this->name = str_replace(SITE_NAME  . '\\', '', get_class($this));
            $this->name = strtolower($this->name);
        }
        return $this->name;
    }

    function widgetTpl($tpl, $data, $args = []){
        if($args){
            $data = array_merge($args, $data);
        }
        $class = loader()->loadWidgetDir($tpl, $data);
        /*
        $class = get_called_class();
        $className = strtolower(str_replace('widget\\', '', $class)) . '/widget/';
        $class = PATH_COM . 'module/' . str_replace('\\', '/', $className);*/
        return $this->view->widgetTpl($tpl, $data, $class);
    }
    
    function tpl($tpl, $data, $args = []){
        if($args){
            $data = array_merge($args, $data);
        }
        return $this->view->tpl($tpl, $data);
    }
}
